﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.Windows.Controls;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Events;
using Microsoft.Practices.Unity;
using Might.GraphicsEditor;
using Might.Infrastructure.Annotations.Graphics;
using Might.Infrastructure.ModelicaDOM;
using Might.Infrastructure.Events;

using Might.Infrastructure.DOM;
using Might.Infrastructure.Services;

namespace Might.ViewsTabControl {
    ///<summary>
    ///</summary>
    public class ViewsTabControlPresenter {
        private readonly IUnityContainer container;
        private IEventAggregator eventAggregator;
        private IViewsTabControlView view;
        private readonly Dictionary<object, TabItem> tabs = new Dictionary<object, TabItem>();

        ///<summary>
        ///</summary>
        ///<param name="container"></param>
        ///<param name="eventAggregator"></param>
        public ViewsTabControlPresenter(IUnityContainer container, IEventAggregator eventAggregator) {
            this.container = container;
            this.eventAggregator = eventAggregator;
            eventAggregator.GetEvent<PackageBrowserItemOpenEvent>().Subscribe(
                PackageBrowserItemOpenEventHandler , ThreadOption.UIThread , false );
        }

        ///<summary>
        ///</summary>
        public IViewsTabControlView View {
            get { return view; }
            set {
                view = value;
                view.SelectionChanged += view_SelectionChanged;
            }
        }

        /// <summary>
        /// Handler for opening item from model treeview.
        /// </summary>
        /// <param name="data"></param>
        public void PackageBrowserItemOpenEventHandler( IDomNode node ) {
            AddItem( node );
        }

        /// <summary>
        /// This function checks, if given object is currently showed and if not, created appropriate editr and add it to
        /// TabControl and select it.
        /// </summary>
        /// <param name="itemData"></param>
        private void AddItem( IDomNode itemData ) {
            
            if (tabs.ContainsKey(itemData)) {
                view.SelectedItem = tabs[itemData];
                
                ((ContainerTabControl)(tabs[itemData]).Content).TabSelected();
            } else {
            
                // Here should be created editor manager insted of direct IconEditor create
                ContainerTabControl cont = new ContainerTabControl();
                TabItem ti = new TabItem {Content = cont };
                
                if( itemData is IModelicaClass ) {
                    
                    StackPanel stack = new StackPanel() { Orientation = Orientation.Horizontal };
                    stack.Children.Add( new TextBlock() { 
                        Text = ( itemData as IModelicaClass ).ClassName , VerticalAlignment = VerticalAlignment.Center 
                    } );
                    Button closeBtn = new Button() { 
                        Content = "x" , Tag = itemData , Margin = new Thickness( 10 , 0 , 0 , 0 ) , Width = 20
                    };
                    closeBtn.Click += new System.Windows.RoutedEventHandler(closeBtn_Click);
                    stack.Children.Add( closeBtn );
                    
                    ti.Header = stack;
                    
                }
                
                tabs.Add(itemData, ti);

              
                // if itemData can have diagram editor
                if (true) {
                    // create diagram editor and add it to ContainerTabControl
                }

                // if itemData can have icon editor
                if (true) {
                    // create icon editor and add it to ContainerTabControl
                    IGraphicsAnnotation ga = container.Resolve<IIconProvider>().GetGraphicsAnnotation( itemData );
                    if (ga != null) {
                        IconEditor drCanvas = new IconEditor(null, ga);

                        TabItem tabItem = new TabItem { Content = drCanvas, Header = "Icon" };
                        cont.AddTab(tabItem);
                    }
                }

                // if itemData can have code editor
                if (true) {
                    // create code editor and add it to ContainerTabControl 
                    CodeEditor drCanvas = new CodeEditor(container, itemData);

                    TabItem tabItem = new TabItem {Content = drCanvas, Header = "Code"};
                    cont.AddTab(tabItem);
                }

                // if itemData can have html viewer
                if (true) {
                    // create html viewer and add it to ContainerTabControl
                }

                // Add ContainerTabControl into view
                view.AddTab(ti);
            }
        }

        private void closeBtn_Click( object sender , System.Windows.RoutedEventArgs e ) {
            
            if( ( sender as Button ).Tag is IDomNode ) {                
                view.RemoveTab( tabs[ ( sender as Button).Tag as IDomNode ] );
                tabs.Remove( ( sender as Button).Tag as IDomNode );            
            } else {                
                throw new Exception( "Tag should be IDomNode" );                
            }
            
        }

        private void view_SelectionChanged(object sender, EventArgs e) {            
            if( view.SelectedItem != null ) {            
                if (((TabItem)view.SelectedItem).Content is ContainerTabControl) {
                    (((TabItem)view.SelectedItem).Content as ContainerTabControl).TabSelected();
                }                
            }
        }
        
    }
}
